package com.bmadmin.service.impl;

import com.bmadmin.domain.*;
import com.bmadmin.mapper.SysDepartmentsPOJODao;
import com.bmadmin.mapper.SysUsersPOJODao;
import com.bmadmin.mapper.SysUsersRolesPOJODao;
import com.bmadmin.service.SysDepartmentService;
import com.bmadmin.service.SysRolesService;
import com.bmadmin.service.SysUsersService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.bmadmin.util.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by SEELE on 2017/12/3.
 */
@Service
@Transactional
public class SysUsersServiceImpl implements SysUsersService {
    @Autowired
    SysUsersPOJODao sysUsersPOJODao;
    @Autowired
    SysDepartmentsPOJODao sysDepartmentsPOJODao;
    @Autowired
    SysUsersRolesPOJODao sysUsersRolesPOJODao;
    @Autowired
    SysRolesService sysRolesService;

    @Override
    public NormalResult insertUser(SysUsersPOJO sysUsersPOJO) throws PinyinException {
        NormalResult result = new NormalResult();

        SysUsersPOJO sysUsersPOJOForQueryId = new SysUsersPOJO();
        sysUsersPOJOForQueryId.setU_id(sysUsersPOJO.getU_id());
        List<SysUsersPOJO> sysUsersPOJOSResultByQueryId = sysUsersPOJODao.find(sysUsersPOJOForQueryId);
        if(sysUsersPOJOSResultByQueryId.size()>0){
            result.setCode("9999");
            result.setMsg("该用户id已经存在");
            return result;
        }

        sysUsersPOJO.setU_password(MD5Util.Md5("123456"));
        sysUsersPOJO.setU_create_date(BMUtil.getNowTimeForString());
        sysUsersPOJO.setU_status(0);//默认用户为正常状态
        sysUsersPOJO.setU_pinyin_daxie(PinyinHelper.getShortPinyin(sysUsersPOJO.getU_name()));
        sysUsersPOJO.setU_pinyin_xiaoxie(PinyinHelper.convertToPinyinString(sysUsersPOJO.getU_name(),"", PinyinFormat.WITHOUT_TONE));

        //将用户与角色关联
        SysUsersRolesPOJO sysUsersRolesPOJO = new SysUsersRolesPOJO();
        sysUsersRolesPOJO.setU_id(sysUsersPOJO.getU_id());
        List<SysUsersRolesPOJO> sysUsersRolesPOJOS = sysUsersRolesPOJODao.find(sysUsersRolesPOJO);
        if(sysUsersRolesPOJOS.size()>0){
            for(SysUsersRolesPOJO item:sysUsersRolesPOJOS){
                sysUsersRolesPOJODao.deleteByU_id(item.getU_id());
            }
        }
        sysUsersRolesPOJO.setR_id(sysUsersPOJO.getR_id());

        sysUsersRolesPOJODao.insertSelective(sysUsersRolesPOJO);

        int insertResult = sysUsersPOJODao.insertSelective(sysUsersPOJO);
        if(insertResult==1){
            result.setCode("0000");
            result.setMsg("用户添加成功");
        }else{
            result.setCode("9999");
            result.setMsg("用户添加失败");
        }
        return result;
    }

    @Override
    public NormalResult updateUser(SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();

        SysUsersRolesPOJO sysUsersRolesPOJO = new SysUsersRolesPOJO();
        sysUsersRolesPOJO.setU_id(sysUsersPOJO.getU_id());
        List<SysUsersRolesPOJO> sysUsersRolesPOJOS = sysUsersRolesPOJODao.find(sysUsersRolesPOJO);
        if(sysUsersRolesPOJOS.size()>0){
            for(SysUsersRolesPOJO item:sysUsersRolesPOJOS){
                sysUsersRolesPOJODao.deleteByU_id(item.getU_id());
            }
        }
        sysUsersRolesPOJO.setR_id(sysUsersPOJO.getR_id());

        sysUsersRolesPOJODao.insertSelective(sysUsersRolesPOJO);

        int updateResult = sysUsersPOJODao.update(sysUsersPOJO);
        if(updateResult==1){
            result.setCode("0000");
            result.setMsg("用户修改成功");
        }else{
            result.setCode("9999");
            result.setMsg("用户修改失败");
        }
        return result;
    }

    @Override
    public NormalResult deleteUser(SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        int deleteResult = sysUsersPOJODao.deletebyU_id(sysUsersPOJO.getU_id());
        if(deleteResult==1){
            result.setCode("0000");
            result.setMsg("用户删除成功");
        }else{
            result.setCode("9999");
            result.setMsg("用户删除失败");
        }
        return result;
    }

    @Override
    public LayUiDataTablePOJO getUsers(SysUsersPOJO sysUsersPOJO) {
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();

        List<SysUsersPOJO> sysUsersPOJOS = sysUsersPOJODao.find(sysUsersPOJO);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setData(sysUsersPOJOS);
        result.setCount(sysUsersPOJOS.size());


        return result;
    }

    @Override
    public NormalResult loginUser(HttpSession httpSession, SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        SysUsersPOJO sysUsersPOJOForQuery = new SysUsersPOJO();
        sysUsersPOJOForQuery.setU_id(sysUsersPOJO.getU_id());
        sysUsersPOJOForQuery.setU_password(MD5Util.Md5(sysUsersPOJO.getU_password()));

        List<SysUsersPOJO> sysUsersPOJOS = sysUsersPOJODao.find(sysUsersPOJOForQuery);
        if(sysUsersPOJOS.size()==0){
            result.setCode("9999");
            result.setMsg("用户名或用户密码错误!");
            return result;
        }
        if(sysUsersPOJOS.get(0).getU_status()==1){
            result.setCode("9999");
            result.setMsg("用户已被停用，请联系系统管理员!");
            return result;
        }

        //查询角色

        SysUsersRolesPOJO sysUsersRolesPOJO = new SysUsersRolesPOJO();
        sysUsersRolesPOJO.setU_id(sysUsersPOJOS.get(0).getU_id());
        List<SysUsersRolesPOJO> sysUsersRolesPOJOS = sysUsersRolesPOJODao.find(sysUsersRolesPOJO);

        //根据角色获取权限列表

        if(sysUsersRolesPOJOS.size()>0){
            SysRolesPOJO sysRolesPOJOForQueryPermission = new SysRolesPOJO();
            sysRolesPOJOForQueryPermission.setR_id(sysUsersRolesPOJOS.get(0).getR_id());
            NormalResult roleByIdIncludePermissionForAsideResult = sysRolesService.findRoleByIdIncludePermissionForAside(sysRolesPOJOForQueryPermission);
            result.setObject(roleByIdIncludePermissionForAsideResult.getObject());
        }


        httpSession.setAttribute(Constants.USER_INFO_SESSION,sysUsersPOJOS.get(0));

        SysUsersPOJO sysUsersPOJOForUpdateLastLoginTime = new SysUsersPOJO();
        sysUsersPOJOForUpdateLastLoginTime.setU_id(sysUsersPOJOS.get(0).getU_id());
        sysUsersPOJOForUpdateLastLoginTime.setU_last_login(BMUtil.getNowTimeForString());
        sysUsersPOJODao.update(sysUsersPOJOForUpdateLastLoginTime);
        result.setCode("0000");
        result.setMsg("用户登录成功");
        return result;
    }

    @Override
    public NormalResult resetPassword(SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        sysUsersPOJO.setU_password(MD5Util.Md5(sysUsersPOJO.getU_password()));
        int updateLines=sysUsersPOJODao.update(sysUsersPOJO);

        if(updateLines==1){
            result.setCode("0000");
            result.setMsg("用户密码修改成功");
        }else {
            result.setCode("9999");
            result.setMsg("用户密码修改异常");
        }
        return result;
    }

    @Override
    public NormalResult getByIdTakeDepartmentAndRoles(SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        List<PluginZtreeDataPOJO> pluginZtreeDataPOJOS = new ArrayList<PluginZtreeDataPOJO>();
        SysUsersPOJO sysUsersPOJOS = sysUsersPOJODao.find(sysUsersPOJO).get(0);

        List<SysDepartmentsPOJO> sysDepartmentsPOJOS = sysDepartmentsPOJODao.find(new SysDepartmentsPOJO());

        for(SysDepartmentsPOJO sysDepartmentsPOJOItem:sysDepartmentsPOJOS){
            PluginZtreeDataPOJO forAddDepartment = new PluginZtreeDataPOJO();

            forAddDepartment.setId(sysDepartmentsPOJOItem.getD_id());
            forAddDepartment.setPart_id(sysDepartmentsPOJOItem.getD_part_id());
            forAddDepartment.setName(sysDepartmentsPOJOItem.getD_id()+" "+sysDepartmentsPOJOItem.getD_name());
            forAddDepartment.setOpen(true);
            if(sysUsersPOJOS.getD_id().equals(sysDepartmentsPOJOItem.getD_id())){
                forAddDepartment.setChecked(true);
            }else {
                forAddDepartment.setChecked(false);
            }

            pluginZtreeDataPOJOS.add(forAddDepartment);
        }

        for(PluginZtreeDataPOJO sysDepartmentsPOJOItemOne:pluginZtreeDataPOJOS){
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();
            for(PluginZtreeDataPOJO sysDepartmentsPOJOItemTwo:pluginZtreeDataPOJOS){
                if(sysDepartmentsPOJOItemOne.getId().equals(sysDepartmentsPOJOItemTwo.getPart_id())){
                    forAddChildren.add(sysDepartmentsPOJOItemTwo);
                }
            }
            sysDepartmentsPOJOItemOne.setChildren(forAddChildren);
        }
        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();
        for(PluginZtreeDataPOJO sysDepartmentsPOJOItem:pluginZtreeDataPOJOS){
            if("00".equals(sysDepartmentsPOJOItem.getPart_id())){
                resultForLevelOne.add(sysDepartmentsPOJOItem);
            }
        }

        sysUsersPOJOS.setDepartmentTree(resultForLevelOne);

        result.setCode("0000");
        result.setMsg("查询成功");
        result.setObject(sysUsersPOJOS);


        return result;
    }
    
    @Override
    public void InsertSysLog(LoginInfoDomain loginInfo){
    	sysUsersPOJODao.InsertSysLog(loginInfo);
    }
    
    @Override
    public List<LoginInfoDomain> getLogList(Map map){
    	return sysUsersPOJODao.getLogList(map);
	}
}
